package winOrDie.graphicsInterface.GameRoom;

import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;

/**
 * This public class is a {@link Composite} that contains Game room options
 *
 */
public class Composite_Options extends Composite {
	
	
	private Button button_Ready_To_play = null;
	private Button button_Captain_Team = null;
	private Button button_Pirates_team = null;	
	private Button button_Start_Game = null;
	private Button button_Voice = null;
	private Button button_Exit = null;
	
	
	private Vector<Composite_Options_Listener> listeners = null;
	
	public synchronized void add_Composite_Options_Listener(Composite_Options_Listener listener)
	{
		listeners.add(listener);
	}
	
	public synchronized void remove_Composite_Options_Listener(Composite_Options_Listener listener)
	{
		listeners.remove(listener);
	}
	
	public void fire_Composite_Options_Event(Composite_Options_Event event)
	{
		for (Composite_Options_Listener listener : listeners) {
			listener.on_Composite_Options_Changed(event);
		}
	}
	
	public Composite_Options(Composite parent, int style) 
	{
		super(parent, style);
		listeners = new Vector<Composite_Options_Listener>();
		initialize();
	}

	private void initialize() 
	{
		GridLayout gridLayout = new GridLayout();
		setLayout(gridLayout);
		
		get_Button_Ready_To_play();
		get_Button_Captain_Team();
		get_Button_Pirates_Team();		
		get_Button_Start_Game();
		get_Button_Voice();
		get_Button_Exit();
	}
	
	public Button get_Button_Ready_To_play()
	{
		if (button_Ready_To_play == null)
		{
			GridData gridData = new GridData();
			gridData.horizontalAlignment = SWT.CENTER;
			gridData.verticalAlignment = SWT.CENTER;
			
			button_Ready_To_play = new Button(this, SWT.PUSH);
			button_Ready_To_play.setLayoutData(gridData);
			
			button_Ready_To_play.setText("Listo para jugar");
			
			final Composite_Options compositeOptions = this;
			
			
			button_Ready_To_play.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_Options_Event event = new Composite_Options_Event(compositeOptions);
					event.setEventType(Composite_Options_Event.Event_Types.READY_TO_PLAY);
					fire_Composite_Options_Event(event);
				}
			});
		}
		return button_Ready_To_play;
	}
	
	
	
	public Button get_Button_Pirates_Team()
	{
		if (button_Pirates_team == null)
		{
			GridData gridData = new GridData();
			gridData.horizontalAlignment = SWT.CENTER;
			gridData.verticalAlignment = SWT.CENTER;
			
			button_Pirates_team = new Button(this, SWT.PUSH);
			button_Pirates_team.setLayoutData(gridData);
			
			button_Pirates_team.setText("Equipo de los piratas");
			
			final Composite_Options compositeOptions = this;
			button_Pirates_team.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_Options_Event event = new Composite_Options_Event(compositeOptions);
					event.setEventType(Composite_Options_Event.Event_Types.TEAM_PIRATES);
					fire_Composite_Options_Event(event);
				}
			});
			
		}
		
		return button_Pirates_team;
	}

	public void show_Button_Pirates_team(final Boolean option)
	{
			Runnable runnable = new Runnable() {
			
			@Override
			public void run() {
				
				getDisplay().asyncExec(new Runnable() {
					
					@Override
					public void run() 
					{
						get_Button_Captain_Team().setVisible(false);
						GridData gridData = ((GridData)get_Button_Pirates_Team().getLayoutData());
						gridData.verticalSpan = SWT.BEGINNING;
						gridData.heightHint = 0;
						gridData.exclude = !option;						
						getShell().update();
						//GridData gd = new GridData()
//						update();
//						redraw();
//						getShell().update();
//						getShell().redraw();
						
					}
				});		
			}
		};
		
		new Thread(runnable).start(); 		
		
	}
		
	public void show_Button_Captain_team(final Boolean option)
	{
//			getDisplay().syncExec(new Runnable() {
//
//				@Override
//				public void run() 
//				{
					GridData gridData = ((GridData)get_Button_Captain_Team().getLayoutData());
					gridData.exclude = !option;
					//get_Button_Captain_Team().setLayoutData(gridData);
					update();
					redraw();
					getDisplay().update();
//				}
//			});		
	}
	
	public Button get_Button_Captain_Team()
	{
		if (button_Captain_Team == null)
		{
			GridData gridData = new GridData();
			gridData.horizontalAlignment = SWT.CENTER;
			gridData.verticalAlignment = SWT.CENTER;
			
			button_Captain_Team = new Button(this, SWT.PUSH);
			button_Captain_Team.setLayoutData(gridData);
			
			button_Captain_Team.setText("Equipo del capitán");
			
			final Composite_Options compositeOptions = this;
			button_Captain_Team.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent arg0) {
					Composite_Options_Event event = new Composite_Options_Event(compositeOptions);
					event.setEventType(Composite_Options_Event.Event_Types.TEAM_CAPTAIN);
					fire_Composite_Options_Event(event);
				}
			});
			
		}
		
		return button_Captain_Team;
	}
	

	Button get_Button_Start_Game()
	{
		if (button_Start_Game == null)
		{
			GridData gridData = new GridData();
			gridData.horizontalAlignment = SWT.CENTER;
			gridData.verticalAlignment = SWT.CENTER;
			
			button_Start_Game = new Button(this, SWT.PUSH);
			button_Start_Game.setLayoutData(gridData);
			
			button_Start_Game.setText("Iniciar partida");
			
			final Composite_Options compositeOptions = this;
			button_Start_Game.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent arg0) {
					Composite_Options_Event event = new Composite_Options_Event(compositeOptions);
					event.setEventType(Composite_Options_Event.Event_Types.FORCE_START);
					fire_Composite_Options_Event(event);
				}
			});
		}
		
		return button_Start_Game;
	}
	
	
	private Button get_Button_Exit()
	{
		if (button_Exit == null)
		{
			GridData gridData = new GridData();
			gridData.horizontalAlignment = SWT.CENTER;
			gridData.verticalAlignment = SWT.CENTER;
			
			button_Exit = new Button(this, SWT.PUSH);
			button_Exit.setLayoutData(gridData);
			
			button_Exit.setText("Salir");
			
			final Composite_Options compositeOptions = this;
			button_Exit.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_Options_Event event = new Composite_Options_Event(compositeOptions);
					event.setEventType(Composite_Options_Event.Event_Types.EXIT);
					fire_Composite_Options_Event(event);
				}
			});
		}
		return button_Exit;
	}
	
	
	
	
	private Button get_Button_Voice()
	{
		if (button_Voice == null)
		{
			GridData gridData = new GridData();
			gridData.horizontalAlignment = SWT.CENTER;
			gridData.verticalAlignment = SWT.CENTER;
			
			button_Voice = new Button(this, SWT.PUSH);
			button_Voice.setLayoutData(gridData);
			
			button_Voice.setText("Voz");
			
			final Composite_Options compositeOptions = this;
			button_Voice.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					Composite_Options_Event event = new Composite_Options_Event(compositeOptions);
					event.setEventType(Composite_Options_Event.Event_Types.VOICE);
					fire_Composite_Options_Event(event);
				}
			});
		}
		return button_Exit;
	}
	
	
	
}
